package io.renren.utils;

import io.renren.entity.ColumnEntity;
import io.renren.entity.ModuleEntity;
import io.renren.entity.TableEntity;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器   工具类
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午11:40:24
 */
public class GenUtils {

    public static List<String> getTemplates(){
        List<String> templates = new ArrayList<String>();
        templates.add("template/Entity.java.vm");
        templates.add("template/EntityRequest.java.vm");
        templates.add("template/Dao.java.vm");
        templates.add("template/Dao.xml.vm");
        templates.add("template/Service.java.vm");
        templates.add("template/ServiceImpl.java.vm");
        templates.add("template/Controller.java.vm");
        templates.add("template/list.html.vm");
        templates.add("template/list.js.vm");
        templates.add("template/menu.sql.vm");
        return templates;
    }
    public static List<String> getTemplates2(){
    	List<String> templates = new ArrayList<String>();
//    	templates.add("template/Bean.xml.vm");
    	templates.add("template/BeanEntity.xml.vm");
    	return templates;
    }

    /**
     * 生成代码
     */
    public static void generatorCode(Map<String, String> table,
                                     List<Map<String, String>> columns, ZipOutputStream zip) {
        //配置信息
        Configuration config = getConfig();
        boolean hasBigDecimal = false;
        //表信息
        TableEntity tableEntity = new TableEntity();
        tableEntity.setTableName(table.get("tableName" ));
        tableEntity.setComments(table.get("tableComment" ));
        //表名转换成Java类名
        String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix" ));
        tableEntity.setClassName(className);
        tableEntity.setClassname(StringUtils.uncapitalize(className));

        //列信息
        List<ColumnEntity> columsList = new ArrayList<>();
        for(Map<String, String> column : columns){
            ColumnEntity columnEntity = new ColumnEntity();
            columnEntity.setColumnName(column.get("columnName" ));
            columnEntity.setDataType(column.get("dataType" ));
            columnEntity.setComments(column.get("columnComment" ));
            columnEntity.setExtra(column.get("extra" ));

            //列名转换成Java属性名
            String attrName = columnToJava(columnEntity.getColumnName());
            columnEntity.setAttrName(attrName);
            columnEntity.setAttrname(StringUtils.uncapitalize(attrName));

            //列的数据类型，转换成Java类型
            String attrType = config.getString(columnEntity.getDataType(), "unknowType" );
            columnEntity.setAttrType(attrType);
            if (!hasBigDecimal && attrType.equals("BigDecimal" )) {
                hasBigDecimal = true;
            }
            //是否主键
            if ("PRI".equalsIgnoreCase(column.get("columnKey" )) && tableEntity.getPk() == null) {
                tableEntity.setPk(columnEntity);
            }

            columsList.add(columnEntity);
        }
        tableEntity.setColumns(columsList);

        //没主键，则第一个字段为主键
        if (tableEntity.getPk() == null) {
            tableEntity.setPk(tableEntity.getColumns().get(0));
        }

        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
        Velocity.init(prop);
        String mainPath = config.getString("mainPath" );
        mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        map.put("tableName", tableEntity.getTableName());
        map.put("comments", tableEntity.getComments());
        map.put("pk", tableEntity.getPk());
        map.put("className", tableEntity.getClassName());
        map.put("classname", tableEntity.getClassname());
        map.put("pathName", tableEntity.getClassname().toLowerCase());
        map.put("columns", tableEntity.getColumns());
        map.put("hasBigDecimal", hasBigDecimal);
        map.put("mainPath", mainPath);
        map.put("package", config.getString("package" ));
        map.put("moduleName", config.getString("moduleName" ));
        map.put("author", config.getString("author" ));
        map.put("email", config.getString("email" ));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        VelocityContext context = new VelocityContext(map);

        //获取模板列表
        List<String> templates = getTemplates();
        for (String template : templates) {
            //渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, "UTF-8" );
            tpl.merge(context, sw);

            try {
                //添加到zip
                zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package" ), config.getString("moduleName" ))));
                IOUtils.write(sw.toString(), zip, "UTF-8" );
                IOUtils.closeQuietly(sw);
                zip.closeEntry();
            } catch (IOException e) {
                throw new RRException("渲染模板失败，表名：" + tableEntity.getTableName(), e);
            }
        }
    }
    /**
     * 生成代码
     */
    public static void generatorCode2(Map<String, String> table,
    		List<Map<String, String>> columns, ZipOutputStream zip) {
    	//配置信息
    	Configuration config = getConfig();
    	boolean hasBigDecimal = false;
    	//表信息
    	TableEntity tableEntity = new TableEntity();
    	tableEntity.setTableName(table.get("tableName" ));
    	tableEntity.setComments(table.get("tableComment" ));
    	//表名转换成Java类名
    	String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix" ));
    	tableEntity.setClassName(className);
    	tableEntity.setClassname(StringUtils.uncapitalize(className));
    	
    	//列信息
    	List<ColumnEntity> columsList = new ArrayList<>();
    	for(Map<String, String> column : columns){
    		ColumnEntity columnEntity = new ColumnEntity();
    		columnEntity.setColumnName(column.get("columnName" ));
    		columnEntity.setDataType(column.get("dataType" ));
    		columnEntity.setComments(column.get("columnComment" ));
    		columnEntity.setExtra(column.get("extra" ));
    		
    		//列名转换成Java属性名
    		String attrName = columnToJava(columnEntity.getColumnName());
    		columnEntity.setAttrName(attrName);
    		columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
    		
    		//列的数据类型，转换成Java类型
    		String attrType = config.getString(columnEntity.getDataType(), "unknowType" );
    		columnEntity.setAttrType(attrType);
    		if (!hasBigDecimal && attrType.equals("BigDecimal" )) {
    			hasBigDecimal = true;
    		}
    		//是否主键
    		if ("PRI".equalsIgnoreCase(column.get("columnKey" )) && tableEntity.getPk() == null) {
    			tableEntity.setPk(columnEntity);
    		}
    		
    		columsList.add(columnEntity);
    	}
    	tableEntity.setColumns(columsList);
    	
    	//没主键，则第一个字段为主键
    	if (tableEntity.getPk() == null) {
    		tableEntity.setPk(tableEntity.getColumns().get(0));
    	}
    	
    	//设置velocity资源加载器
    	Properties prop = new Properties();
    	prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
    	Velocity.init(prop);
    	String mainPath = config.getString("mainPath" );
    	mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
    	//封装模板数据
    	Map<String, Object> map = new HashMap<>();
    	map.put("tableName", tableEntity.getTableName());
    	map.put("comments", tableEntity.getComments());
    	map.put("pk", tableEntity.getPk());
    	map.put("className", tableEntity.getClassName());
    	map.put("classname", tableEntity.getClassname());
    	map.put("pathName", tableEntity.getClassname().toLowerCase());
    	map.put("columns", tableEntity.getColumns());
    	// 开始进行数据的组合
    	List<List> column = new ArrayList<>();
    	List inner = new ArrayList<>();
    	int j=0;
    	boolean skip = false;
    	for(int i=0;i<tableEntity.getColumns().size();i++) {
    		inner.add(tableEntity.getColumns().get(i));
    		j++;
    		if(skip) {
	    		if(j%3==0) {
	    			column.add(new ArrayList(inner));
	    			inner.clear();
	    			j = 0;
	    		}
    		}else {
    			if(j%4==0) {
	    			column.add(new ArrayList(inner));
	    			inner.clear();
	    			j = 0;
	    			skip=true;
	    		}
    		}
    		if(j>0 && (i+1)==tableEntity.getColumns().size()) {
    			column.add(new ArrayList(inner));
    			break;
    		}
    		
    		
    	}
    	map.put("columnsPanel", column);
    	map.put("hasBigDecimal", hasBigDecimal);
    	map.put("mainPath", mainPath);
    	map.put("package", config.getString("package" ));
    	map.put("moduleName", config.getString("moduleName" ));
    	map.put("author", config.getString("author" ));
    	map.put("email", config.getString("email" ));
    	map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
    	VelocityContext context = new VelocityContext(map);
    	
    	//获取模板列表
    	List<String> templates = getTemplates2();
    	for (String template : templates) {
    		//渲染模板
    		StringWriter sw = new StringWriter();
    		Template tpl = Velocity.getTemplate(template, "UTF-8" );
    		tpl.merge(context, sw);
    		
    		try {
    			//添加到zip
    			zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package" ), config.getString("moduleName" ))));
    			IOUtils.write(sw.toString(), zip, "UTF-8" );
    			IOUtils.closeQuietly(sw);
    			zip.closeEntry();
    		} catch (IOException e) {
    			throw new RRException("渲染模板失败，表名：" + tableEntity.getTableName(), e);
    		}
    	}
    }


    /**
     * 列名转换成Java属性名
     */
    public static String columnToJava(String columnName) {
        return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "" );
    }

    /**
     * 表名转换成Java类名
     */
    public static String tableToJava(String tableName, String tablePrefix) {
        if (StringUtils.isNotBlank(tablePrefix)) {
            tableName = tableName.replace(tablePrefix, "" );
        }
        return columnToJava(tableName);
    }

    /**
     * 获取配置信息
     */
    public static Configuration getConfig() {
        try {
            return new PropertiesConfiguration("generator.properties" );
        } catch (ConfigurationException e) {
            throw new RRException("获取配置文件失败，", e);
        }
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, String className, String packageName, String moduleName) {
        String packagePath = "main" + File.separator + "java" + File.separator;
        if (StringUtils.isNotBlank(packageName)) {
            packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;
        }

        if (template.contains("Entity.java.vm" )) {
            return packagePath + "entity" + File.separator + className + ".java";
        }

        if (template.contains("EntityRequest.java.vm" )) {
            return packagePath + "entity" + File.separator +"request"+ File.separator + className + "VO.java";
        }
        
        if (template.contains("Dao.java.vm" )) {
            return packagePath + "dao" + File.separator + className + "Mapper.java";
        }

        if (template.contains("Service.java.vm" )) {
            return packagePath + "service" + File.separator + className + "Service.java";
        }

        if (template.contains("ServiceImpl.java.vm" )) {
            return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
        }

        if (template.contains("Controller.java.vm" )) {
            return packagePath + "controller" + File.separator + className + "Controller.java";
        }

        if (template.contains("Dao.xml.vm" )) {
            return "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + moduleName + File.separator + className + "Mapper.xml";
        }

        if (template.contains("list.html.vm" )) {
            return "main" + File.separator + "resources" + File.separator + "views" + File.separator
                    + "modules" + File.separator + moduleName + File.separator + className.toLowerCase() + ".html";
        }

        if (template.contains("list.js.vm" )) {
            return "main" + File.separator + "resources" + File.separator + "static" + File.separator + "js" + File.separator
                    + "modules" + File.separator + moduleName + File.separator + className.toLowerCase() + ".js";
        }

        if (template.contains("menu.sql.vm" )) {
            return className.toLowerCase() + "_menu.sql";
        }
        if (template.contains("Bean.xml.vm" )) {
        	return className + ".xml";
        }
        if (template.contains("BeanEntity.xml.vm" )) {
//        	return className.toLowerCase() + "Entity.xml";
            return className + ".xml";
        }
        if (template.contains("Menu.xml.vm" )) {
            return className + ".xml";
        }
        if (template.contains("MessagesZh.csv.vm" )) {
            return className + ".xml";
        }

        return null;
    }


    /**
     * 将【表】信息給 TableEntity对象赋值
     * @param tableEntity
     * @param table
     */
    public static void tableEntityParam(TableEntity tableEntity,Map<String, String> table,Configuration config){
        String viewName=String.valueOf(table.get("tableName")).replaceAll("_","-");
        tableEntity.setViewName(viewName);
        tableEntity.setTableName(table.get("tableName"));
        tableEntity.setComments(table.get("tableComment"));
        //表名转换成Java类名
        String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix"));
        tableEntity.setClassName(className);
        tableEntity.setClassname(StringUtils.uncapitalize(className));
        tableEntity.setModulePackage(config.getString("modulePackage"));
        tableEntity.setMoudleName(config.getString("moudleName"));
        tableEntity.setCatalogName(config.getString("catalogName"));
    }

    /**
     * 将【列】信息赋值 给 ColumnEntity 对象
     * @param columnEntity
     * @param column
     * @param config
     * @param tableEntity
     * @param hasBigDecimal
     */
    public static void columnEntityParam(ColumnEntity columnEntity,Map<String, String> column,Configuration config,TableEntity tableEntity,boolean hasBigDecimal){
        columnEntity.setColumnName(column.get("columnName"));
        columnEntity.setDataType(column.get("dataType"));
        columnEntity.setComments(column.get("columnComment"));
        columnEntity.setExtra(column.get("extra"));
        //列名转换成Java属性名
        String attrName = columnToJava(columnEntity.getColumnName());
        columnEntity.setAttrName(attrName);
        columnEntity.setAttrname(StringUtils.uncapitalize(attrName));

        //列的数据类型，转换成Java类型
        String attrType = config.getString(columnEntity.getDataType(), "unknowType");
        columnEntity.setAttrType(attrType);
        if (!hasBigDecimal && attrType.equals("BigDecimal")) {
            hasBigDecimal = true;
        }
        //是否主键
        if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
            tableEntity.setPk(columnEntity);
        }
        columnEntity.setTitleName(strOneUpperCase(String.valueOf(column.get("columnName"))).replaceAll("_"," "));

    }

    /**
     * 将【表及字段】信息赋给 Map
     * @param map
     * @param tableEntity
     */
    public static void templateMapTable(Map<String, Object> map,TableEntity tableEntity){
        map.put("tableName", tableEntity.getTableName());
        map.put("comments", tableEntity.getComments());
        map.put("pk", tableEntity.getPk());
        map.put("className", tableEntity.getClassName());
        map.put("classname", tableEntity.getClassname());
        map.put("pathName", tableEntity.getClassname().toLowerCase());
        map.put("columns", tableEntity.getColumns());
        map.put("viewName",tableEntity.getViewName());
        map.put("modulePackage",tableEntity.getModulePackage());
        map.put("catalogName",tableEntity.getCatalogName());
        map.put("moudleName",tableEntity.getMoudleName());
    }

    public static void columnList(List<List> column,TableEntity tableEntity){
        List inner = new ArrayList<>();
        int j = 0;
        boolean skip = false;
        for (int i = 0; i < tableEntity.getColumns().size(); i++) {
            inner.add(tableEntity.getColumns().get(i));
            j++;
            if (skip) {
                if (j % 3 == 0) {
                    column.add(new ArrayList(inner));
                    inner.clear();
                    j = 0;
                }
            } else {
                if (j % 4 == 0) {
                    column.add(new ArrayList(inner));
                    inner.clear();
                    j = 0;
                    skip = true;
                }
            }
            if (j > 0 && (i + 1) == tableEntity.getColumns().size()) {
                column.add(new ArrayList(inner));
                break;
            }
        }
    }

    /**
     * 将作者信息添加到 表信息Map中
     * @param map
     * @param column
     * @param hasBigDecimal
     * @param mainPath
     * @param config
     */
    public static void templateMapTableAuthor(Map<String, Object> map,List<List> column,boolean hasBigDecimal,String mainPath,Configuration config){
        map.put("columnsPanel", column);
        map.put("hasBigDecimal", hasBigDecimal);
        map.put("mainPath", mainPath);
        map.put("package", config.getString("package"));
        map.put("moduleName", config.getString("moduleName"));
        map.put("author", config.getString("author"));
        map.put("email", config.getString("email"));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
    }

    /**
     * 生成zip文件并输出
     * @param template
     * @param context
     * @param zip
     * @param tableEntity
     * @param config
     */
    public static void outZip(String template,VelocityContext context,ZipOutputStream zip,TableEntity tableEntity,Configuration config){
        //渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8");
        tpl.merge(context, sw);

        try {
            //添加到zip
            zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package"), config.getString("moduleName"))));
            IOUtils.write(sw.toString(), zip, "UTF-8");
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            throw new RRException("渲染模板失败，表名：" + tableEntity.getTableName(), e);
        }
    }
    /**
     * 生成代码
     */
    public static void generatorCodeView(Map<String, String> table,
                                         List<Map<String, String>> columns, ZipOutputStream zip,String template) {
        //配置信息
        Configuration config = getConfig();
        boolean hasBigDecimal = false;
        //表信息
        TableEntity tableEntity = new TableEntity();
        tableEntityParam(tableEntity,table,config);

        String [] col={"id","attrs","version","archived"};
        List<String> list= Arrays.asList(col);
        //列信息
        List<ColumnEntity> columsList = new ArrayList<>();
        for(Map<String, String> column : columns){
            if(!list.contains(column.get("columnName"))){
                ColumnEntity columnEntity = new ColumnEntity();
                columnEntityParam(columnEntity,column,config,tableEntity,hasBigDecimal);
                columsList.add(columnEntity);
            }
        }
        tableEntity.setColumns(columsList);

        //没主键，则第一个字段为主键
        if (tableEntity.getPk() == null) {
            tableEntity.setPk(tableEntity.getColumns().get(0));
        }

        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
        Velocity.init(prop);
        String mainPath = config.getString("mainPath" );
        mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        templateMapTable(map,tableEntity);

        // 开始进行数据的组合
        List<List> column = new ArrayList<>();
        columnList(column,tableEntity);

        templateMapTableAuthor(map,column,hasBigDecimal,mainPath,config);

        VelocityContext context = new VelocityContext(map);

        //渲染模板
        outZip(template,context,zip,tableEntity,config);
    }
    /**
     * 生成代码
     */
    public static void generatorView(Map<String, String> table,
                                         List<Map<String, String>> columns, ZipOutputStream zip,String template) {
        //配置信息
        Configuration config = getConfig();
        boolean hasBigDecimal = false;
        //表信息
        TableEntity tableEntity = new TableEntity();
        tableEntityParam(tableEntity,table,config);
        //列信息
        List<ColumnEntity> columsList = new ArrayList<>();

        String [] col={"id","attrs","version","archived","created_by","updated_on","updated_by"};
        List<String> list= Arrays.asList(col);
        for(Map<String, String> column : columns){
            if(!list.contains(column.get("columnName"))){
                ColumnEntity columnEntity = new ColumnEntity();
                columnEntityParam(columnEntity,column,config,tableEntity,hasBigDecimal);
                columsList.add(columnEntity);
            }
        }
        tableEntity.setColumns(columsList);

        //没主键，则第一个字段为主键
        if (tableEntity.getPk() == null) {
            tableEntity.setPk(tableEntity.getColumns().get(0));
        }

        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
        Velocity.init(prop);
        String mainPath = config.getString("mainPath" );
        mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        templateMapTable(map,tableEntity);

        // 开始进行数据的组合
        List<List> column = new ArrayList<>();
        columnList(column,tableEntity);

        templateMapTableAuthor(map,column,hasBigDecimal,mainPath,config);

        VelocityContext context = new VelocityContext(map);

        //渲染模板
        outZip(template,context,zip,tableEntity,config);
    }


    /**
     * 生成代码
     */
    public static void generatorMenu(List<String> tableNames, ZipOutputStream zip,String template) {
        //配置信息
        Configuration config = getConfig();
        boolean hasBigDecimal = false;

        ModuleEntity moduleEntity=new ModuleEntity();
        moduleEntity.setMoudleName(config.getString("moudleName" ));
        moduleEntity.setModulePackage(config.getString("modulePackage"));
        List<TableEntity> list=new ArrayList<TableEntity>();
        for (String tableName :tableNames){
            TableEntity tableEntity=new TableEntity();
            tableEntity.setTableViewName(tableName.replaceAll("_","-"));
            //表名转换成Java类名
            String className = tableToJava(tableName, config.getString("tablePrefix"));
            tableEntity.setClassName(className);
            list.add(tableEntity);
        }
        moduleEntity.setMenuTables(list);

        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
        Velocity.init(prop);
        String mainPath = config.getString("mainPath" );
        mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
        Map<String, Object> map = new HashMap<>();
        map.put("moudleName", moduleEntity.getMoudleName());
        map.put("modulePackage",moduleEntity.getModulePackage());
        map.put("menuTables", moduleEntity.getMenuTables());
        map.put("hasBigDecimal", hasBigDecimal);
        map.put("mainPath", mainPath);
        map.put("package", config.getString("package" ));
        map.put("moduleName", config.getString("moduleName" ));
        map.put("author", config.getString("author" ));
        map.put("email", config.getString("email" ));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));

        VelocityContext context = new VelocityContext(map);
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8" );
        tpl.merge(context, sw);

        try {
            //添加到zip
            zip.putNextEntry(new ZipEntry(getFileName(template, moduleEntity.getMoudleName(), config.getString("package" ), config.getString("moduleName" ))));
            IOUtils.write(sw.toString(), zip, "UTF-8" );
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            throw new RRException("渲染模板失败，表名：" + moduleEntity.getMoudleName(), e);
        }
    }


    /**
     * 生成代码
     */
    public static void generatorCodeMessages(Map<String, Object> map, ZipOutputStream zip,String template) {

        //配置信息
        Configuration config = getConfig();
        boolean hasBigDecimal = false;

        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
        Velocity.init(prop);
        String mainPath = config.getString("mainPath" );
        mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
        map.put("hasBigDecimal", hasBigDecimal);
        map.put("mainPath", mainPath);
        map.put("package", config.getString("package" ));
        map.put("moduleName", config.getString("moduleName" ));
        map.put("author", config.getString("author" ));
        map.put("email", config.getString("email" ));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));

        VelocityContext context = new VelocityContext(map);
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8" );
        tpl.merge(context, sw);

        try {
            //添加到zip
            zip.putNextEntry(new ZipEntry(getFileName(template, "message", config.getString("package" ), config.getString("moduleName" ))));
            IOUtils.write(sw.toString(), zip, "UTF-8" );
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            throw new RRException("渲染模板失败，表名：" + "message", e);
        }
    }

    /**
     * 生成代码
     */
    public static String generatorCodeMessages111(Map<String, Object> map, String template) {

        //配置信息
        Configuration config = getConfig();
        boolean hasBigDecimal = false;

        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
        Velocity.init(prop);
        String mainPath = config.getString("mainPath" );
        mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
        map.put("hasBigDecimal", hasBigDecimal);
        map.put("mainPath", mainPath);
        map.put("package", config.getString("package" ));
        map.put("moduleName", config.getString("moduleName" ));
        map.put("author", config.getString("author" ));
        map.put("email", config.getString("email" ));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));

        VelocityContext context = new VelocityContext(map);
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8" );
        tpl.merge(context, sw);
        return sw.toString();
    }

    /**
     * 支付串 首字母 大写
     * @param str
     * @return
     */
    public static String strOneUpperCase(String str){
        if(str!=null&&str.length()>0){
            return str.substring(0,1).toUpperCase()+str.substring(1);
        }
        return str;
    }
}
